package com.rdc.qingkou.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.ui.CommentActivity;
import com.rdc.qingkou.ui.OtherHomepageActivity;
import com.rdc.qingkou.ui.PersonalHomepageActivity;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.view.fragment.TwoLevelCommentFragment;
import com.rdc.qingkou.vo.CommentTwoLevelVO;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


public class TwoLevelCommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int TYPE_RECENT_COMMENT = 0;
    private final static int TYPE_NONE = 1;

    private List<CommentTwoLevelVO> mRecentCommentList;
    private CommentActivity mCommentActivity;
    private TwoLevelCommentFragment mTwoLevelCommentFragment;
    private int mReviewerId = 0;

    public TwoLevelCommentRecyclerViewAdapter(CommentActivity activity,
                                              TwoLevelCommentFragment fragment,
                                              List<CommentTwoLevelVO> RecentCommentList) {
        mTwoLevelCommentFragment = fragment;
        mRecentCommentList = RecentCommentList;
        mCommentActivity = activity;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_RECENT_COMMENT) {
            view = LayoutInflater.from(mCommentActivity).inflate(R.layout.item_comment_recyclerview, parent, false);
            RecentCommentViewHolder holder = new RecentCommentViewHolder(view);
            setLongClickListener(view, holder);
            return holder;
        } else {
            view = LayoutInflater.from(mCommentActivity).inflate(R.layout.item_comment_none, parent, false);
            return new NoneViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RecentCommentViewHolder) {
            setCommentData((RecentCommentViewHolder) holder, mRecentCommentList.get(position));
            setClickListener(position, (RecentCommentViewHolder) holder);
        }
    }

    @Override
    public int getItemCount() {
        return mRecentCommentList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mRecentCommentList.size() != 0) {
            return TYPE_RECENT_COMMENT;
        } else {
            return TYPE_NONE;
        }
    }

    /**
     * 给itemView中的各种view设置监听事件
     *
     * @param location iew在数据源List中对应的位置
     * @param holder   view所对应的ViewHolder
     */
    private void setClickListener(final int location, final RecentCommentViewHolder holder) {
        final CommentTwoLevelVO comment = mRecentCommentList.get(location);
        holder.headImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (comment.getUser().getUserId() == MyApplication.getUser().getUserId()) {
                    Intent intent = new Intent(mCommentActivity, PersonalHomepageActivity.class);
                    mCommentActivity.startActivity(intent);
                } else {
                    Intent intent = new Intent(mCommentActivity, OtherHomepageActivity.class);
                    intent.putExtra("user", comment.getUser());
                    mCommentActivity.startActivity(intent);
                }
            }
        });
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.headImage.performClick();
            }
        });
        holder.reviewer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCommentActivity, OtherHomepageActivity.class);
                intent.putExtra("userId", comment.getReviewer().getUserId());
                mCommentActivity.startActivity(intent);
            }
        });
        holder.appreciate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MyUtils.isNetworkConnected(mCommentActivity)) {
                    mTwoLevelCommentFragment.presenter.appreciate(comment.getCommentId());
                } else {
                    Toast.makeText(mCommentActivity, "无网络！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        holder.appreciateNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.appreciate.performClick();
            }
        });
        holder.comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (comment.getUser().getUserId() != MyApplication.getUser().getUserId()) {
                    String hintText = "回复" + comment.getUser().getNickName() + ":";
                    TwoLevelCommentFragment f = (TwoLevelCommentFragment)
                            mCommentActivity.getSupportFragmentManager().findFragmentById(R.id.comment_fragment);
                    f.editText.setHint(hintText);
                    mReviewerId = comment.getUser().getUserId();
                }
            }
        });
        holder.commentNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.comment.performClick();
            }
        });
    }

    /**
     * 给itemView设置长按监听
     */
    private void setLongClickListener(final View view, final RecentCommentViewHolder viewHolder) {
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                PopupMenu popupMenu = new PopupMenu(mCommentActivity, view);
                popupMenu.getMenuInflater().inflate(R.menu.two_level_comment, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.menu_copy:
                                ClipboardManager cm = (ClipboardManager)
                                        mCommentActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData cd = ClipData.newPlainText("Label", viewHolder.content.getText());
                                if (cm != null) {
                                    cm.setPrimaryClip(cd);
                                }
                                Toast.makeText(mCommentActivity, "已经复制到剪切板", Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.menu_answer:
                                TwoLevelCommentFragment f = (TwoLevelCommentFragment)
                                        mCommentActivity.getSupportFragmentManager().findFragmentById(R.id.comment_fragment);
                                if (viewHolder.userId != MyApplication.getUser().getUserId()) {
                                    String hintText = "回复" + viewHolder.name.getText() + ":";
                                    f.editText.setHint(hintText);
                                    mReviewerId = viewHolder.userId;
                                } else {
                                    f.editText.setHint("添加评论...");
                                }
                                break;
                            case R.id.menu_report:
                                break;

                        }
                        return true;
                    }
                });
                popupMenu.show();
                return true;
            }
        });
    }

    /**
     * 给itemView加载数据
     */
    private void setCommentData(RecentCommentViewHolder holder, CommentTwoLevelVO comment) {
        Glide.with(mCommentActivity).load(comment.getUser().getHeadImageUrl()).into(holder.headImage);
        holder.name.setText(comment.getUser().getNickName());
        holder.content.setText(comment.getContent());
        holder.date.setText(comment.getCommentDate());
        holder.appreciateNum.setText(String.valueOf(comment.getPraiseNumber()));
        if (comment.getPraiseNumber() > 0) {
            holder.appreciate.setImageResource(R.drawable.button_appreciate_pressure);
        } else {
            holder.appreciate.setImageResource(R.drawable.button_appreciate_empty);
        }
        holder.commentNum.setText(String.valueOf(comment.getCommentsNumber()));
        if (comment.getReviewer() != null) {
            holder.answerMark.setVisibility(View.VISIBLE);
            holder.reviewer.setVisibility(View.VISIBLE);
            holder.reviewer.setText(comment.getReviewer().getNickName());
        } else {
            holder.answerMark.setVisibility(View.GONE);
            holder.reviewer.setVisibility(View.GONE);
        }
        holder.userId = comment.getUser().getUserId();
    }

    private static class RecentCommentViewHolder extends RecyclerView.ViewHolder {

        CircleImageView headImage;
        TextView name;
        TextView content;
        TextView date;
        ImageView comment;
        ImageView appreciate;
        TextView commentNum;
        TextView appreciateNum;
        TextView answerMark;//回复两个字
        TextView reviewer;//被回复的人
        int userId;

        RecentCommentViewHolder(View itemView) {
            super(itemView);
            headImage = (CircleImageView) itemView.findViewById(R.id.item_comment_head_imageView);
            name = (TextView) itemView.findViewById(R.id.item_comment_name_textView);
            content = (TextView) itemView.findViewById(R.id.item_comment_content_textView);
            date = (TextView) itemView.findViewById(R.id.item_comment_date_textView);
            comment = (ImageView) itemView.findViewById(R.id.item_comment_comment_imageVIew);
            appreciate = (ImageView) itemView.findViewById(R.id.item_comment_appreciate_imageView);
            commentNum = (TextView) itemView.findViewById(R.id.item_comment_commentNum_textView);
            appreciateNum = (TextView) itemView.findViewById(R.id.item_comment_appreciateNum_textView);
            answerMark = (TextView) itemView.findViewById(R.id.item_comment_answer_mark_textView);
            reviewer = (TextView) itemView.findViewById(R.id.item_comment_answered_textView);
        }
    }

    private static class NoneViewHolder extends RecyclerView.ViewHolder {
        NoneViewHolder(View itemView) {
            super(itemView);
        }
    }

    public int getReviewerId() {
        return mReviewerId;
    }

    public void setReviewerId(int mReviewerId) {
        this.mReviewerId = mReviewerId;
    }

}
